/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.demo.ui.snap;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

/**
 * 截图完成后的操作提示
 */
public class OperationDialog extends JDialog implements ActionListener {
    public static final String UNDO_IMG = "images/snap_undo.png";
    public static final String CLOSE_IMG = "images/snap_close.png";
    public static final String SAVE_IMG = "images/snap_save.png";
    public static final String OK_IMG = "images/snap_ok.png";

    private JToolBar operBar;
    private JButton btnUndo;
    private JButton btnClose;
    private JButton btnSave;
    private JButton btnOk;
    private JFileChooser fileChooser;

    public OperationDialog(Frame owner) {
        super(owner);
        init();
    }

    // UI 初始化
    private void init() {

        operBar = new JToolBar();

        btnUndo = new JButton(new ImageIcon(getResource(UNDO_IMG)));
        btnClose = new JButton(new ImageIcon(getResource(CLOSE_IMG)));
        btnSave = new JButton(new ImageIcon(getResource(SAVE_IMG)));
        btnOk = new JButton(new ImageIcon(getResource(OK_IMG)));

        btnUndo.setToolTipText("撤销");
        btnClose.setToolTipText("关闭");
        btnSave.setToolTipText("保存");
        btnOk.setToolTipText("完成");

        // 不可浮动
        operBar.setFloatable(false);
        operBar.add(btnUndo);
        operBar.add(btnClose);
        operBar.add(btnSave);
        operBar.add(btnOk);

        // 不绘制焦点
        btnUndo.setFocusPainted(false);
        btnClose.setFocusPainted(false);
        btnSave.setFocusPainted(false);
        btnOk.setFocusPainted(false);

        this.getContentPane().add(operBar);

        // 去除 title
        this.setUndecorated(true);

        // 自动调整大小
        this.pack();


        /** 文件选择 */
        fileChooser = new JFileChooser();

        // 禁用选择所有
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("jpg(*.jpeg,*.jpg)", "jpg"));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("gif(*.gif)", "gif"));
        FileNameExtensionFilter pngFilter = new FileNameExtensionFilter("png(*.png", "png");
        fileChooser.addChoosableFileFilter(pngFilter);

        // 默认 org.vacoor.nothing.web.filter
        fileChooser.setFileFilter(pngFilter);

        // 禁止多选
        fileChooser.setMultiSelectionEnabled(false);

        this.btnSave.addActionListener(this);
    }

    private URL getResource(String location) {
        URL url = Thread.currentThread().getContextClassLoader().getResource(location);
        if (url == null) {
            url = getClass().getClassLoader().getResource(location);
        }

        if (url == null) {
            url = getClass().getResource(location);
        }

        return url;
    }

    // 事件监听
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnSave) {
            fileChooser.setSelectedFile(null);
            fileChooser.setDialogTitle("Save Image");
            if (JFileChooser.APPROVE_OPTION != fileChooser.showSaveDialog(null)) {
                return;
            }
            File selectedFile = fileChooser.getSelectedFile();
        }
        if (e.getSource() == btnUndo) {
        }
    }
}
